// pages/order/order.js
Page({

    data: {
        showLoadingState:false,

        //搜索栏数据
        p10:"https://pic.imgdb.cn/item/62f9aa2a16f2c2beb174caea.png",
        p11:"https://pic.imgdb.cn/item/63144c8e16f2c2beb10e7e39.png",
        searchInput:"",


        //背景图片
        p1:"https://pic.imgdb.cn/item/62e47e15f54cd3f937c25ed5.png",
        p2:"https://pic.imgdb.cn/item/62e47e15f54cd3f937c25edd.png",
        p3:"https://pic.imgdb.cn/item/62e47e15f54cd3f937c25f31.png",
        p4:"https://pic.imgdb.cn/item/62e47e15f54cd3f937c25f44.png",
        p5:"https://pic.imgdb.cn/item/62e47e15f54cd3f937c25ecf.png",
        p6:"https://pic.imgdb.cn/item/62f4753d16f2c2beb1cda2d2.png",
        p7:"https://pic.imgdb.cn/item/62f4753d16f2c2beb1cda2d5.png",
        sportsPicUrlArray:["https://pic.imgdb.cn/item/632e762116f2c2beb18e9710.png",
        "https://pic.imgdb.cn/item/632e762216f2c2beb18e97c6.png",
        "https://pic.imgdb.cn/item/632e762216f2c2beb18e9840.png",
        "https://pic.imgdb.cn/item/632e762316f2c2beb18e996b.png",
        "https://pic.imgdb.cn/item/632e762016f2c2beb18e95d0.png",
        "https://pic.imgdb.cn/item/632e764e16f2c2beb18ec96f.png",
        "https://pic.imgdb.cn/item/632e764e16f2c2beb18ec98d.png",
        "https://pic.imgdb.cn/item/632e764e16f2c2beb18ec955.png",],
        //数据转换
        room_states:['匹配中','待评价','已完成'],
        //筛选所需数据
        all_array:['所有订单'],
        time_array:['所有时间','今天','昨天','三天内','四天内','五天内','六天内','七天内','八天内','九天内','十天内','十一天内','十二天内','十三天内','十四天内','十五天内','十六天内','十七天内','十八天内','十九天内','二十天内','二十一天内','二十二天内','二十三天内','二十四天内','二十五天内','二十六天内','二十七天内','二十八天内','二十九天内','三十天内','三十一天内','三十二天内','三十三天内','三十四天内','三十五天内','三十六天内'],
        location_array:['所有场地','大学生活动中心','共青场','二附中操场'],
        state_array:['所有状态','匹配中','待评价','已完成'],
        state_array2:['匹配中','待评价','已完成'],

        search_state_array:['所有订单','筛选时间','筛选状态'],
        search_array:['所有订单'],
        search_state_index:0,
        search_index:0,



        //根据不同时间段，不同场馆，不同状态可以筛选出不同订单（也就是房间）
        rooms:[{
            room_id:123,
            room_location:'大活乒乓球馆',
            room_playground:'场地一',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            room_sports:1,
            room_all_person:5,//现在房间有多少人
            room_state:1,//分为匹配中，待评价，已完成三种，用0，1，2分别代替
        },
        {
            room_id:123,
            room_location:'大活乒乓球馆',
            room_playground:'场地一',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            room_sports:2,
            room_all_person:5,//现在有多少人
            room_state:1,//分为匹配中，待评价，已完成三种，用0，1，2分别代替
        },
        {
            room_id:123,
            room_location:'大活乒乓球馆',
            room_playground:'场地一',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            room_sports:3,
            room_all_person:5,//现在有多少人
            room_state:1,//分为匹配中，待评价，已完成三种，用0，1，2分别代替
        },
        {
            room_id:123,
            room_location:'大活乒乓球馆',
            room_playground:'场地一',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            room_sports:4,
            room_all_person:5,//现在有多少人
            room_state:1,//分为匹配中，待评价，已完成三种，用0，1，2分别代替
        },
        {
            room_id:123,
            room_location:'大活乒乓球馆',
            room_playground:'场地一',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            room_sports:5,
            room_all_person:5,//现在有多少人
            room_state:1,//分为匹配中，待评价，已完成三种，用0，1，2分别代替
        },
        {
            room_id:123,
            room_location:'大活乒乓球馆',
            room_playground:'场地一',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            room_sports:6,
            room_all_person:5,//现在有多少人
            room_state:1,//分为匹配中，待评价，已完成三种，用0，1，2分别代替
        },
        {
            room_id:123,
            room_location:'大活乒乓球馆',
            room_playground:'场地一',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            room_sports:7,
            room_all_person:5,//现在有多少人
            room_state:1,//分为匹配中，待评价，已完成三种，用0，1，2分别代替
        }
    ]
    },
    onShow: function (options) {
        var that = this;
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"SHOW_ORDERS",
              Id:getApp().globalData.userInfo.studentId,
            },
            success(res){
              if(res.data == false){
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作
                  console.log(res);
                  that.setData({
                      "rooms":res.data.orderList,
                  })
              }
            },
            fail(res){
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
          });
    },
    toPage_detailed_order_page:function(e){
        var x = e.currentTarget.dataset.roomid;
        wx.navigateTo({
          url: '../detailed-order-page/detailed-order-page?roomid='+x,
        })
    },
    onSearchStateChange:function(e){
        var x = e.detail.value;
        this.setData({
            'search_state_index':x,
            'search_index':0,
        });
        var that = this;
        that.onShowLoading();
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"SHOW_ORDERS",
              Id:getApp().globalData.userInfo.studentId,
            },
            success(res){
              if(res.data == false){
                  that.onHideLoading()
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  that.onHideLoading()
                  //成功获取到信息后的操作
                  console.log(res);
                  that.setData({
                      "rooms":res.data.orderList,
                  })
              }
            },
            fail(res){
                that.onHideLoading()
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
          });

        if(x == 0){
                var array = this.data.all_array;
                this.setData({
                    'search_array':array,
                });
        }
        else if(x == 1){
                //time_array    
                var array = this.data.time_array;
                console.log(1);
                this.setData({
                    [`search_array`]:array,
                });
        }
        else if(x == 2){
            var that = this;
            this.setData({
                [`search_array`]:this.data.location_array,
            });
        }
        else if(x ==3){
            this.setData({
                [`search_array`]:this.data.state_array,
            });
        }

    },
    onSearchChange:function(e){
        var that = this;
        var x = e.detail.value;
        this.setData({
            'search_index':x,
        });
        var stateIndex = that.data.search_state_index;
        //筛选时间
        if(stateIndex == 1){
            if(that.data.search_index == 0){
                //获取所有信息
                wx.request({
                    url: getApp().globalData.serverUrl,
                    method:"GET",
                    timeout:10000,
                    data:{
                      //各种数据的传送
                      operation:"SHOW_ORDERS",
                      Id:getApp().globalData.userInfo.studentId,
                    },
                    success(res){
                      if(res.data == false){
                          wx.showModal({
                              showCancel:false,
                              title:"请求失败,请重试",
                          });
                      }
                      else{
                          //成功获取到信息后的操作
                          console.log(res);
                          that.setData({
                              "rooms":res.data.orderList,
                          })
                      }
                    },
                    fail(res){
                      wx.showModal({
                          showCancel:false,
                          title:"请求超时,请重试",
                      });
                    },
                  });
            }
            else{
                var that = this;
                that.onShowLoading()
                wx.request({
                  url: getApp().globalData.serverUrl,
                  method:"GET",
                  timeout:10000,
                  data:{
                      //a1570670347
                    //各种数据的传送
                    operation:"SEARCH_ORDERS",
                    Id:getApp().globalData.userInfo.studentId,
                    searchConditionType:"time",
                    searchConditionValue:that.getDateString(that.data.search_index),
                  },
                  success(res){
                      console.log(res)
                    if(res.data == false){
                        that.onHideLoading()
                        wx.showModal({
                            showCancel:false,
                            title:"请求失败,请重试",
                        });
                    }
                    else{
                        //成功获取到信息后的操作
                        
                        that.onHideLoading()
                        that.setData({
                            'rooms':res.data.orderList,
                        })
                    }
                  },
                  fail(res){
                      console.log(res)
                      that.onHideLoading()
                    wx.showModal({
                        showCancel:false,
                        title:"请求超时,请重试",
                    });
                  },
                });
            }

        }
        else if(stateIndex == 2){//筛选状态
            var that = this;
            that.onShowLoading()
            wx.request({
              url: getApp().globalData.serverUrl,
              method:"GET",
              timeout:10000,
              data:{
                //各种数据的传送
                operation:"SEARCH_ORDERS",
                Id:getApp().globalData.userInfo.studentId,
                searchConditionType:"state",
                searchConditionValue:that.data.search_index - 1,
              },
              success(res){
                if(res.data == false){
                    that.onHideLoading();
                    wx.showModal({
                        showCancel:false,
                        title:"请求失败,请重试",
                    });
                }
                else{
                    //成功获取到信息后的操作
                    that.onHideLoading()
                    that.setData({
                        "rooms":res.data.orderList,
                    })
                }
              },
              fail(res){
                  that.onHideLoading()
                wx.showModal({
                    showCancel:false,
                    title:"请求超时,请重试",
                });
              },
            });
        }
    },
    getDateString:function(e){
        var date = new Date();
        date.setTime(date.getTime()-24*60*60*1000*(e-1));
        var targetDate = date.getFullYear() + '-' +(date.getMonth()+1) + '-' + date.getDate();
        console.log(targetDate);
        return targetDate;
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
    onChangeInput:function(e){
        var x = e.detail.value;
        this.setData({
            "searchInput":x,
        })
    },
    onSearchOrder:function(e){
        var that = this;
        var text = that.data.searchInput;
        that.onShowLoading();
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"SEARCH_ORDER",
              Id:getApp().globalData.userInfo.studentId,
              roomId:text,
            },
            success(res){
              if(res.data == false){
                  that.onHideLoading();
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
                  console.log(res)
              }
              else{
                  that.onHideLoading()
                  //成功获取到信息后的操作
                  console.log(res);
                  that.setData({
                      "rooms":res.data.orderList,
                  })
              }
            },
            fail(res){
                console.log(res)
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
          });
    }
    }
)